﻿/***********************************************************************
 * <copyright file="TreeGridExtension.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Thursday, November 14, 2013
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System.Globalization;
using System.Web.Mvc;

namespace EasyUIMvc.Tree
{
    public class TreeGridExtension : ExtensionBase
    {
        private readonly TreeGridSettings _settings;
        public TreeGridExtension(TreeGridSettings settings)
        {
            _settings = settings;
        }

        public override string GetScripts()
        {
            return "";
        }

        public override MvcHtmlString GetHtml()
        {
            _settings.DataOptions += "url: " + _settings.DataOption.UrlJsonDataSource + ",";

            if (!string.IsNullOrEmpty(_settings.DataOption.LoadFilterFunction))
            {
                _settings.DataOptions += "loadFilter: " + _settings.DataOption.LoadFilterFunction + ",";
            }
            _settings.DataOptions += "rownumbers: " + _settings.DataOption.ShowRowNumber.ToString().ToLower() + ",";
            _settings.DataOptions += "idField: '" + _settings.DataOption.FieldID + "',";
            _settings.DataOptions += "treeField: '" + _settings.DataOption.TreeField + "',";
            _settings.DataOptions += "method: '" + _settings.DataOption.Method + "',";
            _settings.DataOptions += "fitColumns: " + _settings.DataOption.AutoFitComlumn.ToString().ToLower();

            if (!string.IsNullOrEmpty(_settings.DataOption.ContextMenu))
            {
                _settings.DataOptions += ", onContextMenu: " + _settings.DataOption.ContextMenu;
            }
            if (!string.IsNullOrEmpty(_settings.DataOption.OnDblClickRow))
            {
                _settings.DataOptions += ", onDblClickRow: " + _settings.DataOption.OnDblClickRow;
            }
            if (!string.IsNullOrEmpty(_settings.DataOption.OnLoadSuccess))
            {
                _settings.DataOptions += ", onLoadSuccess: " + _settings.DataOption.OnLoadSuccess;
            }
            var treeGrid = GetTopTag("table", "easyui-treegrid", _settings);
            treeGrid.MergeAttribute("style", _settings.Style);
            treeGrid.MergeAttribute("title", _settings.Title);
            treeGrid.InnerHtml += "<thead>  <tr>";

            for (int i = 0; i < _settings.DataOption.TreeColumnName.Length; i++)
            {
                var tag = new TagBuilder("th");
                tag.Attributes.Add("data-options", "field:'" + _settings.DataOption.TreeColumnName[i] + "'");
                if (_settings.DataOption.TreeColumnWidth[i]!= 0)
                {
                    tag.Attributes.Add("width", _settings.DataOption.TreeColumnWidth[i].ToString(CultureInfo.InvariantCulture));
                }
                if (!string.IsNullOrEmpty(_settings.DataOption.TreeColumnTextAlign[i]))
                {
                    tag.Attributes.Add("align", _settings.DataOption.TreeColumnTextAlign[i]);
                }
                
                tag.InnerHtml += _settings.DataOption.TreeColumnText[i];
                treeGrid.InnerHtml += tag;
            }
            

            treeGrid.InnerHtml += " </tr> </thead> ";

            return MvcHtmlString.Create(treeGrid.ToString());
        }
    }

    public class TreeGridSettings : SettingsBase
    {
        public string Title { get; set; }

        public string Style { get; set; }

        public TreeGridDataOption DataOption { get; set; }
    }

    public class TreeGridDataOption
    {
        public string ContextMenu { get; set; }
        public string OnDblClickRow { get; set; }
        public string OnLoadSuccess { get; set; }
        public string UrlJsonDataSource { get; set; }
        public string Method { get; set; }
        public bool ShowRowNumber { get; set; }
        public string FieldID { get; set; }
        public string TreeField { get; set; }
        public string LoadFilterFunction { get; set; }
        public bool AutoFitComlumn { get; set; }
        public string[] TreeColumnName { get; set; }
        public string[] TreeColumnText { get; set; }
        public short[] TreeColumnWidth { get; set; }
        public string[] TreeColumnTextAlign { get; set; }
    }
}
